summaryrefslogtreecommitdiff
path: root/apps/web/src/app/blog/[slug]/page.tsx
blob: 46c6163d943f6c3e426f11d7484255883e3810f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { Metadata } from "next";
import Image from "next/image";
import { notFound } from "next/navigation";
import { BasePage } from "@/app/base-page";
import Prose from "@/components/ui/prose";
import { Separator } from "@/components/ui/separator";
import { getPosts, getSinglePost, processHtmlContent } from "@/lib/blog/query";
import type { Author, Post } from "@/types/blog";

type PageProps = {
	params: Promise<{ slug: string }>;
	searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
};

export async function generateMetadata({
	params,
}: PageProps): Promise<Metadata> {
	const slug = (await params).slug;

	const data = await getSinglePost({ slug });

	if (!data || !data.post) return {};

	return {
		title: data.post.title,
		description: data.post.description,
		twitter: {
			title: `${data.post.title}`,
			description: `${data.post.description}`,
			card: "summary_large_image",
			images: [
				{
					url: data.post.coverImage,
					width: "1200",
					height: "630",
					alt: data.post.title,
				},
			],
		},
		openGraph: {
			type: "article",
			images: [
				{
					url: data.post.coverImage,
					width: "1200",
					height: "630",
					alt: data.post.title,
				},
			],
			title: data.post.title,
			description: data.post.description,
			publishedTime: new Date(data.post.publishedAt).toISOString(),
			authors: data.post.authors.map((author: Author) => author.name),
		},
	};
}

export async function generateStaticParams() {
	const data = await getPosts();
	if (!data || !data.posts.length) return [];

	return data.posts.map((post) => ({
		slug: post.slug,
	}));
}

export default async function BlogPostPage({ params }: PageProps) {
	const slug = (await params).slug;
	const data = await getSinglePost({ slug });
	if (!data || !data.post) return notFound();

	const html = await processHtmlContent({ html: data.post.content });

	return (
		<BasePage>
			<PostHeader post={data.post} />
			<Separator />
			<PostContent html={html} />
		</BasePage>
	);
}

function PostHeader({ post }: { post: Post }) {
	const formattedDate = new Date(post.publishedAt).toLocaleDateString("en-US", {
		day: "numeric",
		month: "long",
		year: "numeric",
	});

	return (
		<>
			{post.coverImage && <PostCoverImage post={post} />}
			<PostMeta date={formattedDate} publishedAt={post.publishedAt} />
			<PostTitle title={post.title} />
			<PostAuthor author={post.authors[0]} />
		</>
	);
}

function PostCoverImage({ post }: { post: Post }) {
	return (
		<div className="relative aspect-video overflow-hidden rounded-lg">
			<Image
				src={post.coverImage}
				alt={post.title}
				loading="eager"
				fill
				className="rounded-lg object-cover"
			/>
		</div>
	);
}

function PostMeta({ date, publishedAt }: { date: string; publishedAt: Date }) {
	return (
		<div className="flex items-center justify-center">
			<time dateTime={publishedAt.toString()}>{date}</time>
		</div>
	);
}

function PostTitle({ title }: { title: string }) {
	return (
		<h1 className="text-5xl font-bold tracking-tight md:text-4xl">{title}</h1>
	);
}

function PostAuthor({ author }: { author?: Author }) {
	if (!author) return null;

	return (
		<div className="flex items-center justify-center gap-2">
			<Image
				src={author.image}
				alt={author.name}
				width={36}
				height={36}
				loading="eager"
				className="aspect-square size-8 shrink-0 rounded-full"
			/>
			<p className="text-muted-foreground">{author.name}</p>
		</div>
	);
}

function PostContent({ html }: { html: string }) {
	return (
		<section className="pt-8">
			<Prose html={html} />
		</section>
	);
}